package com.java.web.manageweb.controller;

import com.java.base.pojo.EasyUIResult;
import com.java.manage.pojo.TbContent;
import com.java.manage.service.TbContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping(value = "/rest/content")
public class ContentController {
    @Autowired
    private TbContentService contentService;

   /*
    * 添加广告内容
    * 
    * @author Tigger Wu
    * @date 2018/9/12 14:00
    * @param [content]
    * @return org.springframework.http.ResponseEntity<java.lang.Void>
    */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveContent(TbContent content) {
        try {
            Date d = new Date();
            content.setCreated(d);
            content.setUpdated(d);
            content.setId(null);
            contentService.insert(content);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

   /*
    *  根据内容分类id查询分类列表
    *
    * @author Tigger Wu
    * @date 2018/9/12 14:00
    * @param [categoryId, page, rows]
    * @return org.springframework.http.ResponseEntity<com.java.base.pojo.EasyUIResult>
    */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult> queryListByCategoryId(@RequestParam("categoryId") Integer categoryId,
                                                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                              @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        try {
            EasyUIResult easyUIResult = contentService.queryListByCategoryId(categoryId,page,rows);
            return ResponseEntity.ok(easyUIResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


}
